package edu.cmu.cs.stage3.alice.core.response.visualization.array;

import edu.cmu.cs.stage3.alice.core.Model;
import edu.cmu.cs.stage3.alice.core.property.NumberProperty;
import edu.cmu.cs.stage3.alice.core.visualization.ArrayOfModelsVisualization;

public class SetItemAtIndex extends ArrayVisualizationWithItemAnimation {

 public final NumberProperty index = new NumberProperty( this, "index", new Integer( -1 ) );
 public class RuntimeSetValue extends RuntimeArrayVisualizationWithItemAnimation {
  private ArrayOfModelsVisualization m_subject;
  private Model m_item;

  //Rem @Override
  public void prologue( double t ) {
   super.prologue( t );
   m_subject = subject.getArrayOfModelsVisualizationValue();
   m_item = item.getModelValue();
   if( m_item != null ) {

    //todo?
    m_item.visualization.set( null );

   }
  }
  //Rem @Override
  public void update( double t ) {
   super.update( t );
  }
  //Rem @Override
  public void epilogue( double t ) {
   super.epilogue( t );
   //todo
   int indexValue = index.intValue();
   //LOB 'donwsize' portability - to compile under Java 1.4
   //L Model[] array = m_subject.getItems().clone(); // ArrayOfModelsVisualization -> CollectionOfModelsVisualization: Model[] getItems()
   Model[] array = (Model[])(m_subject.getItems().clone()); // ArrayOfModelsVisualization -> CollectionOfModelsVisualization: Model[] getItems()
   for( int i=0; i<array.length; i++ ) {
    if( array[ i ] == m_item ) { // 'erase' items that are equals
     array[ i ] = null;
     }
    }
   array[ indexValue ] = m_item;
   m_subject.setItems( array ); // ArrayOfModelsVisualization m_subject
  }
 }

}
